using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RainEngine.SceneManagement
{
    public class PostProcessor
    {
        GraphicsDevice graphicsDevice;
        Effect ppEffect;        
        VertexPositionTexture[] ppVertices;

        public PostProcessor(GraphicsDevice setGraphicsDevice)
        {
            graphicsDevice = setGraphicsDevice;
            ppEffect = RainGE.PPEffect;
            InitPostProcessingVertices();
        }

        private void InitPostProcessingVertices()
        {
            ppVertices = new VertexPositionTexture[4];
            ppVertices[0] = new VertexPositionTexture(new Vector3(-1, 1, 0f), new Vector2(0, 0));
            ppVertices[1] = new VertexPositionTexture(new Vector3(1, 1, 0f), new Vector2(1, 0));
            ppVertices[2] = new VertexPositionTexture(new Vector3(-1, -1, 0f), new Vector2(0, 1));
            ppVertices[3] = new VertexPositionTexture(new Vector3(1, -1, 0f), new Vector2(1, 1));
        }

        public void Draw(List<string> ppEffectsList,RenderTarget2D RT1,RenderTarget2D RT2)
        {
            for (int currentTechnique = 0; currentTechnique < ppEffectsList.Count; currentTechnique++)
            {
                if (currentTechnique % 2 == 0)
                {
                    if (currentTechnique == 0)
                    {
                        graphicsDevice.SetRenderTarget(RT2);
                        ppEffect.Parameters["originalImage"].SetValue(RT1);
                        ppEffect.Parameters["textureToSampleFrom"].SetValue(RT1);
                    }
                    if (currentTechnique == ppEffectsList.Count - 1)
                        graphicsDevice.SetRenderTarget(null);
                    else
                    {
                        graphicsDevice.SetRenderTarget(RT2);
                        ppEffect.Parameters["textureToSampleFrom"].SetValue(RT1);
                    }                               
                }
                else
                {
                    if (currentTechnique == ppEffectsList.Count - 1)
                        graphicsDevice.SetRenderTarget(null);
                    else
                        graphicsDevice.SetRenderTarget(RT1);

                    ppEffect.Parameters["textureToSampleFrom"].SetValue(RT2);                    
                }
                DrawRenderTarget2D(ppEffectsList, currentTechnique);
            }
        }

        private void DrawRenderTarget2D(List<string> ppEffectsList, int currentTechnique)
        {
            ppEffect.CurrentTechnique = ppEffect.Techniques[ppEffectsList[currentTechnique]];
            foreach (EffectPass pass in ppEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, ppVertices, 0, 2);
            }
        }

        public EffectParameterCollection Parameters
        {
            get { return ppEffect.Parameters; }
        }
    }
}
